package com.java110.mall.goods.cmd.assetInventory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.asset.AssetInventoryDto;
import com.java110.dto.shop.ShopHouseDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.goods.IAssetInventoryInnerServiceSMO;
import com.java110.intf.goods.IAssetInventoryProductInnerServiceSMO;
import com.java110.intf.shop.IShopHouseInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.assetInventory.AssetInventoryPo;
import com.java110.po.assetInventory.AssetInventoryProductPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
@Java110Cmd(serviceCode = "/assetInventory/saveAssetInventory")
public class SaveAssetInventoryCmd extends Cmd {

    @Autowired
    private IAssetInventoryInnerServiceSMO assetInventoryInnerServiceSMOImpl;

    @Autowired
    private IAssetInventoryProductInnerServiceSMO assetInventoryProductInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userInnerServiceSMOImpl;

    @Autowired
    private IShopHouseInnerServiceSMO shopHouseInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "invTime", "请求报文中未包含盘点时间");
        Assert.hasKeyAndValue(reqJson, "shId", "请求报文中未包含仓库");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含商铺");

        String userId = CmdContextUtils.getUserId(cmdDataFlowContext);
        reqJson.put("staffId", userId);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {


        //保存盘点 产品
        if (!reqJson.containsKey("inoutOrderProducts")) {
            throw new IllegalArgumentException("未包含盘点产品");
        }

        JSONArray inoutOrderProducts = reqJson.getJSONArray("inoutOrderProducts");

        if (inoutOrderProducts == null || inoutOrderProducts.size() < 1) {
            throw new IllegalArgumentException("未包含盘点产品");
        }

        AssetInventoryPo assetInventoryPo = BeanConvertUtil.covertBean(reqJson, AssetInventoryPo.class);
        assetInventoryPo.setAiId(GenerateCodeFactory.getGeneratorId("11"));
        List<AssetInventoryProductPo> assetInventoryProductPos = new ArrayList<>();
        for (int productIndex = 0; productIndex < inoutOrderProducts.size(); productIndex++) {
            dealSaveAssetInventoryProduct(inoutOrderProducts.getJSONObject(productIndex), reqJson, assetInventoryPo, assetInventoryProductPos);
        }

        assetInventoryPo.setState(AssetInventoryDto.STATE_DOING);

        //查询用户信息
        UserDto userDto = new UserDto();
        userDto.setUserId(reqJson.getString("staffId"));
        List<UserDto> userDtos = userInnerServiceSMOImpl.queryUsers(userDto);

        Assert.listOnlyOne(userDtos, "员工不存在");
        assetInventoryPo.setStaffName(userDtos.get(0).getName());

        //查询仓库
        ShopHouseDto shopHouseDto = new ShopHouseDto();
        shopHouseDto.setShopId(assetInventoryPo.getShopId());
        shopHouseDto.setShId(assetInventoryPo.getShId());
        List<ShopHouseDto> shopHouses = shopHouseInnerServiceSMOImpl.queryShopHouses(shopHouseDto);

        Assert.listOnlyOne(shopHouses, "仓库不存在");
        assetInventoryPo.setShName(shopHouses.get(0).getShName());

        int flag = assetInventoryProductInnerServiceSMOImpl.saveAssetInventoryProducts(assetInventoryProductPos);

        if (flag < 1) {
            throw new IllegalArgumentException("保存失败");
        }

        flag = assetInventoryInnerServiceSMOImpl.saveAssetInventory(assetInventoryPo);
        if (flag < 1) {
            throw new IllegalArgumentException("保存失败");
        }
        cmdDataFlowContext.setResponseEntity(ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功"));
    }


    /**
     * 保存盘点产品
     *
     * @param jsonObject
     * @param reqJson
     * @param assetInventoryPo
     */
    private void dealSaveAssetInventoryProduct(JSONObject jsonObject, JSONObject reqJson, AssetInventoryPo assetInventoryPo, List<AssetInventoryProductPo> assetInventoryProductPos) {
        AssetInventoryProductPo assetInventoryProductPo = BeanConvertUtil.covertBean(jsonObject, AssetInventoryProductPo.class);
        assetInventoryProductPo.setAiId(assetInventoryPo.getAiId());
        assetInventoryProductPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
        assetInventoryProductPo.setState(AssetInventoryDto.STATE_DOING);
        assetInventoryProductPos.add(assetInventoryProductPo);
    }
}
